package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.views.LocalTerritoryElectricityMeter;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.ChangeLocalTerritoryElectricityMeterParentMeterFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.ElectricityMeterFormsRequestProcessor;

public class GetAndFillChangeLocalTerritoryElectricityMeterParentMeterFormWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		ElectricityMeterFormsRequestProcessor electricityMeterFormsRequestProcessor = null;
		ChangeLocalTerritoryElectricityMeterParentMeterFormConverter changeLocalTerritoryElectricityMeterParentMeterFormConverter = null;
		LocalTerritoryElectricityMeter localTerritoryElectricityMeter = null;
		ElectricityMeterService electricityMeterService = null;
		List<ElectricityMeter> listOfPotentialParentElectricityMetersForLocalTerritoryMeter = null;
		electricityMeterFormsRequestProcessor = new ElectricityMeterFormsRequestProcessor();
		
		int localTerritoryElectricityMeterIDWhichParentMeterToReplace = 
				electricityMeterFormsRequestProcessor.getLocalTerritoryElectricityMeterIDWhichParentMeterToReplace(request);
		
		if (localTerritoryElectricityMeterIDWhichParentMeterToReplace != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			electricityMeterService = new ElectricityMeterServiceImpl();
			listOfPotentialParentElectricityMetersForLocalTerritoryMeter = new ArrayList<ElectricityMeter>();
			
			localTerritoryElectricityMeter = electricityMeterService.getLocalTerritoryElectricityMeterByID(
					localTerritoryElectricityMeterIDWhichParentMeterToReplace);
			listOfPotentialParentElectricityMetersForLocalTerritoryMeter = electricityMeterService.getListOfPotentialParentElectricityMeters(
					localTerritoryElectricityMeterIDWhichParentMeterToReplace);
			
			if (localTerritoryElectricityMeter != null)
			{
				changeLocalTerritoryElectricityMeterParentMeterFormConverter = 
						new ChangeLocalTerritoryElectricityMeterParentMeterFormConverter();
				changeLocalTerritoryElectricityMeterParentMeterFormConverter.fillWithInitialData(
						localTerritoryElectricityMeter, listOfPotentialParentElectricityMetersForLocalTerritoryMeter);
				
				request.setAttribute("changeLocalTerritoryElectricityMeterParentMeterFormConverter", 
						changeLocalTerritoryElectricityMeterParentMeterFormConverter);
				
				HttpSession session = request.getSession();
				session.setAttribute("localTerritoryElectricityMeter", localTerritoryElectricityMeter);
				session.setAttribute("listOfPotentialParentElectricityMetersForLocalTerritoryMeter", 
						listOfPotentialParentElectricityMetersForLocalTerritoryMeter);
	
				operationResult = OPERATION_SUCCESSFUL;
			}
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}

		return operationResult;
	}
}